package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public Role findById(Integer id) {
        return roleDao.findAll(id);
    }

    @Override
    public void add(Role role, Integer[] permissionIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        this.setRoleAndPermission(roleId,permissionIds);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds) {
        roleDao.edit(role);
        roleDao.deleteAssociation(role.getId());
        this.setRoleAndPermission(role.getId(),permissionIds);
    }

    @Override
    public void delete(Integer id) {
        Long userCount = roleDao.selectCountByUserId(id);
        Long menuCount = roleDao.selectCountByMenu(id);
        if(userCount > 0 ){
            throw new RuntimeException(MessageConstant.DELETE_PERMISSION_FALL_USER);
        }else if(menuCount > 0){
            throw new RuntimeException(MessageConstant.DELETE_PERMISSION_FALL_MENU);
        }else {
            roleDao.deletePermissionByRoleId(id);
            roleDao.deleteById(id);
        }
    }

    @Override
    public List<Role> findAllRole() {
        return roleDao.findAllRole();
    }

    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);

        Page<Role> page = roleDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    @Override
    public List<Integer> findPermissionIdsByRole(Integer permissionIds) {
        return permissionDao.findPermissionIdsByRole(permissionIds);

    }

    @Override
    public List<Integer> findRolesByMenuId(Integer menuId) {
        return roleDao.findRolesByMenuId(menuId);
    }

    private void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if(permissionIds !=null &&permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }
}
